package com.picbrown.scrape;

import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

public class Scraper {

	private static final String ROW_CLASS2 = "<tr class=\"offwhite\">";
	private static final String ROW_CLASS1 = "<tr class=\"white\">";
	private static final String CITY_FILTER = "<td>Hartwell, GA</td>";
	private static final String prefix = "<form name=\"search_results\" method=\"get\" action=\"/privatelabel/listing/pl_boat_detail_handler.jsp?\"> "
            + "<input type=\"hidden\" name=\"ps\" value=\"100\" />" 
            + "<input type=\"hidden\" name=\"slim\" value=\"pp284456\" /> "
            + "<input type=\"hidden\" name=\"units\" value=\"Feet\" /> "
            + "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">" 
              + "<tbody>"
               + "<tr>" 
                 
                + "<td align=\"right\"> <span class=\"feature\">Page:&nbsp;&nbsp; <b>1</b>&nbsp;</span> </td>" 
               + "</tr>" 
               + "<tr> "
                + "<td colspan=\"2\">" 
                 + "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" summary=\"search_results\" width=\"100%\">" 
                  + "<tbody>";
	private static final String suffix = "</tbody>"
             + "</table>" 
             + "<input type=\"button\" class=\"hidden\" style=\"margin-top: 4px; " +
             "\" name=\"CheckAll\" value=\"Check All\" onclick=\"checkAll(document.search_results.checked)\" />" 
             + "<input type=\"button\" class=\"hidden\" style=\"margin-top: 4px; " +
             "\" name=\"UnCheckAll\" value=\"Uncheck All\" onclick=\"uncheckAll(document.search_results.checked)\" />" 
             +"<br />" 
             + "<input type=\"submit\" class=\"hidden\" style=\"margin-top: 4px; " +
             "\" name=\"details\" value=\"Show Details\" /> "
             +"<br /> "
            + "</form>";

	public static void main(String[] args) throws IOException {
		Document doc = connect("http://www.c2cyacht.com/search.php?slim=pp284456&searched=true&city=Hartwell&lineonly&ps=100");
		String title = doc.title();		
		//System.out.println(title);
		Element body = doc.body();	
		System.out.println(body);
		Element search = body.getElementById("Full_Container").getElementById("Content_Container").getElementById("Hidden_Search");
		Element content = body.getElementById("Full_Container").getElementById("Content_Container").getElementById("content");
		Element tableSchema = search.getElementById("Search_Body");
		Elements tables = content.getElementsByTag("table");
		//System.out.println("no of tables: " + tables.size());
		Elements table = tables.get(0).child(0).child(0).child(0).
				getElementsByTag("table").select("[summary=search_results]");
		List<Node> rows = table.get(0).child(0).childNodes();
		//printRows(rows);
		//System.out.println(rows);
	}

	private static void printRows(List<Node> rows) {
		System.out.println(prefix);
		String current = ROW_CLASS2;
		String next = ROW_CLASS1;
		for (Node node : rows) {
			//System.out.println("=====================================================");
			String nodeString = node.toString();
			if(!nodeString.isEmpty()){
				if(nodeString.contains(ROW_CLASS1) || nodeString.contains(ROW_CLASS2)){
					if(nodeString.contains(CITY_FILTER)){
						if(nodeString.contains(next)){
							System.out.println(node);
						}else{
							System.out.println(nodeString.replaceFirst(current, next));
						}
						String t = current;
						current = next;
						next = t;
					}
				}else{
					System.out.println(node);
				}
			}
			//System.out.println("=====================================================");
		}
		System.out.println(suffix);
	}

	private static Document connect(String url) throws IOException {
		Document doc = Jsoup.connect(url).get();
		return doc;
	}
}
